package com.exam.service.impl;

import java.rmi.StubNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.exam.exception.StudentNonExist;
import com.exam.mapper.ClassesMapper;
import com.exam.mapper.MajorMapper;
import com.exam.mapper.StudentMapper;
import com.exam.pojo.Classes;
import com.exam.pojo.Paging;
import com.exam.pojo.Student;
import com.exam.service.StudentService;
import com.exam.util.MD5Util;
import com.exam.util.SnoUtil;

/**
 * @author 郭荣锋
 * 学生服务层
 */
@Service("studentServiceImpl")
public class StudentServiceImpl implements StudentService {
	
	@Resource(name="studentMapper")
	private StudentMapper studentMapper;
	@Resource(name="majorMapper")
	private MajorMapper majorMapper;
	@Resource(name="classesMapper")
	private ClassesMapper classesMapper;
	@Resource(name="paging")
	Paging<Student> paging;
	@Resource(name="map")
	private Map map;
	@Resource(name="student")
	private Student student;
	
	/**
	  *  根据学生学号获取学生对象
	 * @param sno 学号 
	 * @return 学生对象
	 */
	@Override
	public Student getStudentBySno(String sno) {
		return studentMapper.getStudentBySno(sno);
	}

	/**
	  *  添加学生
	 * @param map 包括 studentName 学生姓名，majorName专业名称，classesName 班级名称，password 密码
	 * @return void
	 */
	@Override
	public void addStudent(Map map) {
		String studentName = (String)map.get("studentName");
		String classesName = (String)map.get("classesName");
		String majorName = (String)map.get("majorName");
		String password = (String)map.get("password");
		Integer majorId = majorMapper.getMajorByName(majorName).getId(); //专业编号
		map.clear();
		map.put("majorId",majorId);
		map.put("classesName",classesName);
		Classes classes =classesMapper.getClassesByMajorIdAndName(map); //获取班级对象
		student.setStudentName(studentName);//填入学生姓名
		student.setPassword(password); //填入密码
		student.setClasses(classes); //填入班级对象
		student.setStatus(1); //填入身份值
		Integer seatNum = studentMapper.getSeatNumberInClasses(student); //座位号
		String sno = SnoUtil.createSno(majorId, classes.getClassesName(),seatNum); //学号
		student.setSno(sno); //填入学号
		studentMapper.addStudent(student);
	}
	
	/**
	 * 删除学生
	 * @param student 学生对象
	 * @return void
	 */
	@Override
	public void deleteStudent(Student student) {
		studentMapper.deleteStudent(student); 
	}

	/**
	 * 更新学生信息 
	 * @param student 学生信息
	 * @return void
	 */
	@Override
	public void updateStudent(Map map) {
		Integer studentId = (Integer)map.get("studentId");
		Student student = studentMapper.getStudentById(studentId); //获取要修改的学生对象
		String studentName = (String)map.get("studentName"); //学生姓名
		String password = (String)map.get("password"); //密码
		String majorName = (String)map.get("majorName"); //专业名称
		String classesName = (String)map.get("classesName"); //班级名称
		//获取classes对象
		Integer majorId = majorMapper.getMajorByName(majorName).getId();
		//System.out.println(majorId);
		Map classesMap = new HashMap<Object,Object>(); //获取班级对象的参数列表
		classesMap.put("classesName",classesName);
		classesMap.put("majorId",majorId);
		Classes classes = classesMapper.getClassesByMajorIdAndName(classesMap);
		System.out.println(classes.getId());
		student.setClasses(classes);
		if(password.equals(student.getPassword())) { //密码没有修改
			student.setPassword(password);
		} else {
			student.setPassword(MD5Util.toMd5(password));
		}
		student.setStudentName(studentName);
		studentMapper.updateStudentById(student);
		
	}

	/**
	 * 根据班级编号获取学生列表
	 * @param classesId 班级编号
	 * @return List<Student> 学生列表
	 */
	@Override
	public List<Student> getStudentByClassesId(Integer classesId) {
		return studentMapper.getStudentByClassesId(classesId);
	}

	/**
	 * 通过专业名称获取学生列表
	 * @param majorName 专业名称
	 * @return List<Student> 学生列表
	 */
	@Override
	public List<Student> getStudentByMajorName(String majorName) {
		Integer majorId = majorMapper.getMajorByName(majorName).getId();
		return studentMapper.getStudentByMajorId(majorId);
	}
	
	/**
	  *  学生列表分页
	 * @param type 分页类型(1.普通,2.按专业,3.按班级)
	 * @param map 参数列表(必选:pageSize 分页大小,page 当前页;  可选:majorName 专业名称,classesName 班级名称)
	 * @return 已分页的学生列表
	 */
	@Override
	public Paging<Student> studentPaging(Map<Object, Object> map) {
		List<Student> studentList = null; //学生列表
		Map param = new HashMap(); //参数列表
		Integer pageSize = (Integer)map.get("pageSize");
		Integer page = (Integer)map.get("page");
		String majorName = null; //专业名称
		String classesName = null; //班级名称
		Integer majorId = null;
		Map totalCountMap = new HashMap<Object,Object>(); //计算总记录数的参数列表
		
		if(!map.get("majorName").equals("")) { //如果专业名称有传递过来
			majorName = (String)map.get("majorName");
			//转换成专业ID
			majorId = majorMapper.getMajorByName(majorName).getId();
			param.put("majorId",majorId);
			totalCountMap.put("majorId",majorId);
		} else {
			param.put("majorId",null);
		}
		if(!map.get("classesName").equals("")) { //如果班级名称存在
			classesName = (String)map.get("classesName");
			majorName = (String)map.get("majorName");
			System.out.println("majorName="+majorName);
			//转换成班级ID
			majorId = majorMapper.getMajorByName(majorName).getId();
			Map classesMap = new HashMap<Object,Object>();
			classesMap.put("majorId",majorId);
			classesMap.put("classesName",classesName);
			Classes classes = classesMapper.getClassesByMajorIdAndName(classesMap);
			Integer classesId = classes.getId();//test
			param.put("classesId",classesId);
			totalCountMap.put("classesId",classesId);
		}
		//填充参数列表
		param.put("start",((Integer)map.get("page")-1)*(Integer)map.get("pageSize"));
		param.put("offset", map.get("pageSize"));
		Integer totalCount = studentMapper.getTotalCount(totalCountMap);
		paging.setTotalCount(totalCount); //设置总记录数	
		paging.setCurrentPage(page); //设置当前页
		paging.setPageSize(pageSize); //设置页面大小
		studentList = studentMapper.getStudentByPaging(param);
		paging.setList(studentList);//设置该页的列表
		return paging;
	}
	
	/**
	  * 获取学生总记录数
	 * @param map 参数列表(可选:majorId 专业编号,classesId 班级编号)
	 * @return 总记录数
	 */
	@Override
	public Integer getTotalCount(Map<Object,Object> map) {
		return studentMapper.getTotalCount(map);
	}

	/**
	 * 通过学生编号获取学生对象
	 * @param studentId 学生编号
	 * @return 学生对象
	 */
	@Override
	public Student getStudentById(Integer studentId) {
		return studentMapper.getStudentById(studentId);
	}

	/**
	 * 以分页的形式获取被删除的学生列表
	 * @param map 可选:majorName 专业名称 classesName 班级名称 
	 * @return 被删除学生列表
	 */
	@Override
	public Paging<Student> getDeletedStudentByPaging(Map<Object, Object> map) {
		List<Student> studentList = null; //学生列表
		Map param = new HashMap(); //参数列表
		Integer pageSize = (Integer)map.get("pageSize");
		Integer page = (Integer)map.get("page");
		String majorName = null; //专业名称
		String classesName = null; //班级名称
		Integer majorId = null;
		Map totalCountMap = new HashMap<Object,Object>(); //计算总记录数的参数列表
		
		if(!map.get("majorName").equals("")) { //如果专业名称有传递过来
			majorName = (String)map.get("majorName");
			//转换成专业ID
			majorId = majorMapper.getMajorByName(majorName).getId();
			param.put("majorId",majorId);
			totalCountMap.put("majorId",majorId);
		} else {
			param.put("majorId",null);
		}
		if(!map.get("classesName").equals("")) { //如果班级名称存在
			classesName = (String)map.get("classesName");
			majorName = (String)map.get("majorName");
			System.out.println("majorName="+majorName);
			//转换成班级ID
			majorId = majorMapper.getMajorByName(majorName).getId();
			Map classesMap = new HashMap<Object,Object>();
			classesMap.put("majorId",majorId);
			classesMap.put("classesName",classesName);
			Classes classes = classesMapper.getClassesByMajorIdAndName(classesMap);
			Integer classesId = classes.getId();//test
			param.put("classesId",classesId);
			totalCountMap.put("classesId",classesId);
		}
		//填充参数列表
		param.put("start",((Integer)map.get("page")-1)*(Integer)map.get("pageSize"));
		param.put("offset", map.get("pageSize"));
		Integer totalCount = studentMapper.getDeletedStudenTotalCount(totalCountMap);
		paging.setTotalCount(totalCount); //设置总记录数	
		paging.setCurrentPage(page); //设置当前页
		paging.setPageSize(pageSize); //设置页面大小
		//studentList = studentMapper.getStudentByPaging(param);
		studentList = studentMapper.getDeletedByPaging(param);
		paging.setList(studentList);//设置该页的列表
		return paging;
	}

	/**
	 * 恢复被删除的学生
	 * @param studentId 学生编号 
	 * @return void
	 */
	@Override
	public void recoverStudent(Integer studentId) {
		Student student = studentMapper.getDeletedStudentById(studentId);
		student.setState(1);
		studentMapper.updateStudent(student);
	}
	
}
